<template>
  <div class>
    <div class="card tr mb2"></div>

    <div class="card form" v-show="formShow">
      <el-button size="mini" type="primary" icon="el-icon-plus" @click="showAddDeploy">新增</el-button>
    </div>

    <el-table ref="dragTable" row-key="id" :data="dataList" border style="width: 100%">
      <el-table-column type="index" width="60px" fixed="left" align="center" label="序号"></el-table-column>
      <el-table-column v-for="(item,index) in allItemList" :key="index" :label="item.name" v-if="item.show">
        <template slot-scope="scope">
          <template v-if="!item.formatter">{{ scope.row[item.key] }}</template>
          <template v-else>
            <span v-text="item.formatter(scope.row, item)"></span>
          </template>
        </template>
      </el-table-column>

      <el-table-column width="250px" label="操作" fixed="right" >
        <template slot-scope="scope">
          <el-button size="mini" type="success" @click="openDiagram(scope.row)">设计</el-button>
          <el-button v-if="scope.row.tmplStatus==1" size="mini" type="success" @click="flowDeploy(scope.row)">流程发布</el-button>
          <el-button size="mini" type="success" @click="delFlowTmpl(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="card" style="text-align:right;">
      <el-pagination
        @size-change="sizeChange"
        @current-change="currentChange"
        :current-page="currentPage"
        :page-sizes="[ 10, 20,30, 40]"
        :page-size="pageSize"
        background
        layout="total, sizes, prev, pager, next"
        :total="total"
      ></el-pagination>
    </div>

    <el-dialog title="新增流程" :visible.sync="dialogAddVisible" center>
      <div class="card form">
        <el-form :model="tmplForm" label-width="180px">
          <el-form-item label="流程发布名称">
            <el-input v-model="tmplForm.tmplName"></el-input>
          </el-form-item>
          <el-form-item label="流程KEY">
            <el-input v-model="tmplForm.tmplKey"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="addFlowTmpl" type="primary">保存</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>

    <el-dialog title="流程设计器" :visible="diagramDialogVisible" :fullscreen="true">
      <diagram
        :tmplId="tmplForm.id" 
        @saveBpmn="saveBpmn" 
        @closeDiagram="closeDiagram" 
      ></diagram>
    </el-dialog>
  </div>
</template>
<script>
import { getFlowTmplList, addFlowTmpl,editFlowTmpl,delFlowTmpl, deploy } from "./function";
import diagram from "../diagram/index";

const defaultFormColumn = [
  {
    name: "流程key",
    key: "tmplKey",
    align: "center",
    show: true
  },
  {
    name: "流程模板名称",
    key: "tmplName",
    align: "center",
    show: true
  },
  {
    name: "创建时间",
    key: "createTime",
    align: "center",
    show: true
  },
  {
    name: "状态",
    key: "tmplStatus",
    align: "center",
    formatter: row => {
      if (row.tmplStatus == 0) {
        return "新建";
      }
      if (row.tmplStatus == 1) {
        return "已就绪";
      }
      else if (row.tmplStatus == 2) {
        return "编辑中";
      } 
      else if (row.tmplStatus == 3) {
        return "已发布";
      } 
      else if (row.tmplStatus == 4) {
        return "已终止";
      } 
      else {
        return "其他";
      }
    },
    show: true,
  }
];

export default {
  components: {
    diagram
  },
  data() {
    return {
      modalDel: false,
      formShow: true,
      dialogAddVisible: false,
      diagramDialogVisible: false,
      tmplForm: {
        tmplName: "",
        tmplKey: "",
        bpmnXml: "",
        bpmnSvg: "",
        tmplStatus: "0"
      },
      total: 0,
      pageSize: 10,
      currentPage: 1,
      loading: true,
      allItemList: defaultFormColumn,
      dataList: []
    };
  },

  mounted() {
    this.initData();
  },
  methods: {
    showAddDeploy() {
      this.dialogAddVisible = true;
    },

    addFlowTmpl() {
      addFlowTmpl(this.tmplForm).then(res => {
        if (res.retHead.code == "0000000") {
          this.$message({
            message: "操作成功！",
            type: "success"
          });
          this.dialogAddVisible = false;
          this.initData();
        } else {
          this.$message({
            message: "操作失败！",
            type: "error"
          });
        }
      });
    },
    openDiagram(row) {
      this.diagramDialogVisible = true;
      this.tmplForm.id = row.id;
    },
    initData() {
      let params = {
        pageNum: this.currentPage,
        pageSize: this.pageSize
      };
      getFlowTmplList(params).then(res => {
        if (res.retHead.code == "0000000") {
          this.dataList = res.retBody.list;
          this.total = res.retBody.total;
          this.currentPage = res.retBody.pageNum;
        }
      });
    },
    /**
     * 发布
     */
    flowDeploy(row) {
      this.$confirm("确定要发布流程吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let params = {
            tmplId: row.id
          };

          deploy(params).then(res => {
            if (res.retHead.code == "0000000") {
              this.$message({
                message: "操作成功！",
                type: "success"
              });
              this.initData();
            } else {
              this.$message({
                message: res.retHead.message,
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    // 保存
    saveBpmn(params) {
      editFlowTmpl(params).then(res => {
        if (res.retHead.code == "0000000") {
          this.$message({
            message: "操作成功！",
            type: "success"
          });
          this.initData();
          this.diagramDialogVisible = false;
        } else {
          this.$message({
            message: "操作失败！",
            type: "error"
          });
        }
      });
    },
   /**
     * 删除
     */
    delFlowTmpl(row) {
      this.$confirm("确定要发布流程吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          let params = {
            tmplId: row.id
          };
          delFlowTmpl(params).then(res => {
            if (res.retHead.code == "0000000") {
              this.$message({
                message: "操作成功！",
                type: "success"
              });
              this.initData();
            } else {
              this.$message({
                message: res.retHead.message,
                type: "error"
              });
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    closeDiagram() {
      this.diagramDialogVisible = false;
    },
    sizeChange(n) {
      this.pageSize = n;
      this.init();
    },
    currentChange(n) {
      this.currentPage = n;
      this.init();
    }
  }
};
</script>
<style lang="scss" scoped>
/deep/ .el-dialog__body {
  padding: 0 !important;
}
</style>
